package com.drug.lib.util;

import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.Collections;
import java.util.Set;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * <p>
 * 创建日期：2022/7/1 or 9:31
 * 描述：
 */
public class SpUtils {
    private static final String ACCOUNT_INFO_TABLE = "ACCOUNT_INFO_TABLE";
    private static SpUtils mInstance;
    private static MMKV ACCOUNTMMKV;

    private SpUtils() {
        ACCOUNTMMKV = MMKV.mmkvWithID(ACCOUNT_INFO_TABLE);
    }

    /*** 初始化MMKV,只需要初始化一次，建议在Application中初始化 **/
    public static SpUtils getInstance() {
        if (mInstance == null) {
            synchronized (SpUtils.class) {
                if (mInstance == null) {
                    mInstance = new SpUtils();
                }
            }
        }
        return mInstance;
    }

    /*** 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法 */
    public static void encode(String key, Object object) {
        if (object instanceof String) {
            ACCOUNTMMKV.encode(key, (String) object);
        } else if (object instanceof Integer) {
            ACCOUNTMMKV.encode(key, (Integer) object);
        } else if (object instanceof Boolean) {
            ACCOUNTMMKV.encode(key, (Boolean) object);
        } else if (object instanceof Float) {
            ACCOUNTMMKV.encode(key, (Float) object);
        } else if (object instanceof Long) {
            ACCOUNTMMKV.encode(key, (Long) object);
        } else if (object instanceof Double) {
            ACCOUNTMMKV.encode(key, (Double) object);
        } else if (object instanceof byte[]) {
            ACCOUNTMMKV.encode(key, (byte[]) object);
        } else {
            ACCOUNTMMKV.encode(key, object.toString());
        }
    }

    public static void encodeSet(String key, Set<String> sets) {
        ACCOUNTMMKV.encode(key, sets);
    }

    public static void encodeParcelable(String key, Parcelable obj) {
        ACCOUNTMMKV.encode(key, obj);
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     */
    public static Integer decodeInt(String key) {
        return ACCOUNTMMKV.decodeInt(key, 0);
    }

    public static Double decodeDouble(String key) {
        return ACCOUNTMMKV.decodeDouble(key, 0.00);
    }

    public static Long decodeLong(String key) {
        return ACCOUNTMMKV.decodeLong(key, 0L);
    }

    public static Boolean decodeBoolean(String key) {
        return ACCOUNTMMKV.decodeBool(key, false);
    }

    public static Float decodeFloat(String key) {
        return ACCOUNTMMKV.decodeFloat(key, 0F);
    }

    public static byte[] decodeBytes(String key) {
        return ACCOUNTMMKV.decodeBytes(key);
    }

    public static String decodeString(String key) {
        return ACCOUNTMMKV.decodeString(key, "");
    }

    public static Set<String> decodeStringSet(String key) {
        return ACCOUNTMMKV.decodeStringSet(key, Collections.<String>emptySet());
    }

    public static Parcelable decodeParcelable(String key) {
        return ACCOUNTMMKV.decodeParcelable(key, null);
    }

    /*** 移除某个key对 ** @param key */
    public static void removeKey(String key) {
        ACCOUNTMMKV.removeValueForKey(key);
    }

    /*** 判断是否包含某个key */
    public static boolean containsKey(String key) {
        return ACCOUNTMMKV.contains(key);
    }

    /*** 清除所有key */
    public static void clearAll() {
        ACCOUNTMMKV.clearAll();
    }
}
